import { createRouter, createWebHashHistory } from "vue-router";
import { mainStore } from "../store";
import { ElNotification } from "element-plus";
const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: "/",
      redirect: {
        name: "Login",
      },
    },
    {
      path: "/Login",
      name: "Login",
      component: () => import("../views/Login.vue"),
    },
    {
      path: "/Manager",
      name: "Manager",
      component: () => import("../views/Manager.vue"),
      children: [
        {
          path: "CategoryInfoList",
          name: "CategoryInfoList",
          component: () => import("../views/categoryInfo/CategoryInfoList.vue"),
          meta: {
            title: "分类列表",
            requireAuth: true,
          },
        },
        {
          path: "AddCategoryInfo",
          name: "AddCategoryInfo",
          component: () => import("../views/categoryInfo/AddCategoryInfo.vue"),
          meta: {
            title: "新增分类",
            autoClose: true,
            requireAuth: true,
          },
        },
        {
          path: "FoodInfoList",
          name: "FoodInfoList",
          component: () => import("../views/FoodInfo/FoodInfoList.vue"),
          meta: {
            title: "菜品列表",
            requireAuth: true,
          },
        },
        {
          path: "UserInfoList",
          name: "UserInfoList",
          component: () => import("../views/Userinfo/UserInfoList.vue"),
          meta: {
            title: "用户列表",
            requireAuth: true,
          },
        },
        {
          path: "addressInfoList",
          name: "addressInfoList",
          component: () => import("../views/addressInfo/addressInfoList.vue"),
          meta: {
            title: "地址列表",
            requireAuth: true,
          },
        },
        {
          path: "OrderInfoList",
          name: "OrderInfoList",
          component: () => import("../views/orderInfo/OrderInfoList.vue"),
          meta: {
            title: "订单列表",
            requireAuth: true,
          },
        },
        {
          path: "AdminInfoList",
          name: "AdminInfoList",
          component: () => import("../views/adminInfo/AdminInfoList.vue"),
          meta: {
            title: "管理员列表",
            requireAuth: true,
          },
        },
        {
          path: "CommentInfoList",
          name: "CommentInfoList",
          component: () => import("../views/commentInfo/CommentInfoList.vue"),
          meta: {
            title: "评论列表",
            requireAuth: true,
          },
        },
        {
          path: "AddFoodInfo",
          name: "AddFoodInfo",
          component: () => import("../views/FoodInfo/AddFoodInfo.vue"),
          meta: {
            title: "新增菜品",
            autoClose: true,
            requireAuth: true,
          },
        },
        {
          path: "EditFoodInfo/:id",
          name: "EditFoodInfo",
          component: () => import("../views/FoodInfo/EditFoodInfo.vue"),
          meta: {
            title: "编辑菜品",
            autoClose: true,
            requireAuth: true,
          },
        },

        {
          path: "EditCategoryInfo/:id",
          name: "EditCategoryInfo",
          component: () => import("../views/categoryInfo/EditCategoryInfo.vue"),
          meta: {
            title: "编辑菜品分类",
            autoClose: true,
            requireAuth: true,
          },
        },
        {
          path: "EditAdminInfo/:id",
          name: "EditAdminInfo",
          component: () => import("../views/adminInfo/EditAdminInfo.vue"),
          meta: {
            title: "编辑管理员",
            autoClose: true,
            requireAuth: true,
          },
        },
        {
          path: "AddAdminInfo",
          name: "AddAdminInfo",
          component: () => import("../views/adminInfo/AddAdminInfo.vue"),
          meta: {
            title: "新增管理员",
            autoClose: true,
            requireAuth: true,
          },
        },
        {
          path: "DateView",
          name: "DateView",
          component: () => import("../views/DateView/DateView.vue"),
          meta: {
            title: "首页",
            requireAuth: true,
          },
        },
      ],
    },
  ],
});

//导航守卫（路由的狗子函数） beforeEach跳转理由之前
//router.beforeEach()

router.beforeEach((to, from, next) => {
  const store = mainStore();
  if (to.meta.requireAuth) {
    //需要登录以后才能进
    if (store.token) {
      //说明有token，放行
      next();
    } else {
      ElNotification({
        type: "warning",
        title: "系统提示",
        message: "您还没有登录，请选去登录",
      });
      next({
        name: "Login",
      });
    }
  } else {
    //不需要登录
    next();
  }
});

//跳转路由之后的
router.afterEach((to, from) => {
  // console.log(to);
  const store = mainStore();
  if (to.meta.title) {
    store.addHistory({
      title: to.meta.title,
      routeName: to.name,
    });
  }
  if (from.meta.autoClose) {
    store.removeHistoryByRouteName(from.name);
  }
});

export default router;
